package pub.dsb.example.rx.operators;

import com.google.common.collect.Lists;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableSource;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Action;
import io.reactivex.rxjava3.observers.DisposableObserver;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import java.util.ArrayList;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version V1.0.0
 * @date 2020/12/23 16:05
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2020/12/23 16:05
 * @modify reason: {方法名}:{原因}
 * ...
 * @since V1.0.0
 */
public class FlatMapOperatorTest {
    /**
     * 处理一批数据
     *
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("DEMO 1 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .subscribe(vals -> {
                    for (String val : vals) {
                        System.out.println(val);
                    }
                });
        System.out.println("DEMO 2 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .subscribe(vals -> {
                    Observable.fromIterable(vals)
                            .subscribe(val -> System.out.println(val));
                });
        System.out.println("DEMO 3 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .flatMap(vals -> Observable.fromIterable(vals))
                .subscribe(val -> System.out.println(val));
        System.out.println("DEMO 4 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .flatMap(vals -> Observable.fromIterable(vals))
                .flatMap(val -> Observable.just(val + ">>"))
                .subscribe(val -> System.out.println(val));
        System.out.println("DEMO 5 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .flatMap(vals -> Observable.fromIterable(vals))
                .flatMap(val -> Observable.just(val + ">>"))
                .take(1)
                .subscribe(val -> System.out.println(val));
        System.out.println("DEMO 6 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .flatMap(vals -> Observable.fromIterable(vals))
                .flatMap(val -> Observable.just(val + ">>"))
                .take(2)
                .filter(p -> p.contains("ld"))
                .subscribe(val -> System.out.println(val));
        System.out.println("DEMO 7 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .flatMap(vals -> Observable.fromIterable(vals))
                .flatMap(val -> Observable.just(val + ">>"))
                .take(2)
                .filter(p -> p.contains("ld"))
                .doOnNext(p -> System.out.println(">>" + p))
                .subscribe(val -> System.out.println(val));
        System.out.println("DEMO 8 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .flatMap(vals -> Observable.fromIterable(vals))
                .flatMap(val -> Observable.just(val + ">>"))
                .take(2)
                .filter(p -> p.contains("ld"))
                .doOnNext(p -> System.out.println(">>" + p))
                .flatMap(FlatMapOperatorTest::nullPointEx)
                .flatMap(val -> Observable.just(val + ">> +1"))
                .subscribe(next -> System.out.println("OK: " + next),
                        error -> System.out.println("RECIEVE: " + error.getMessage()));
        System.out.println("DEMO 9 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .flatMap(vals -> Observable.fromIterable(vals))
                .flatMap(val -> Observable.just(val + ">>"))
                .take(2)
                .filter(p -> p.contains("ll"))
                .doOnNext(p -> System.out.println(">>" + p))
                .flatMap(FlatMapOperatorTest::nullPointEx)
                .flatMap(val -> Observable.just(val + ">> +1"))
                .subscribe(next -> System.out.println("OK: " + next),
                        error -> System.out.println("RECIEVE: " + error.getMessage()));
        System.out.println("DEMO 10 ----");
        Observable.just(Lists.newArrayList("Hello", "World"))
                .flatMap(vals -> Observable.fromIterable(vals))
                .flatMap(val -> Observable.just(val + ">>"))
                .take(2)
                .filter(p -> p.contains("ll"))
                .doOnNext(p -> System.out.println(">>" + p))
                .flatMap(FlatMapOperatorTest::nullPointEx)
                .flatMap(val -> Observable.just(val + ">> +1"))
                .subscribe(next -> System.out.println("OK: " + next),
                        error -> System.out.println("RECIEVE: " + error.getMessage()),
                        () -> {
                            System.out.println("DONE~!");
                        });
    }

    /**
     * 根据条件抛出异常
     *
     * @param p
     * @return
     */
    private static ObservableSource<?> nullPointEx(String p) {
        if (p.contains("ld")) {
            return Observable.error(new NullPointerException("NULL EX"));
        }
        return Observable.just(p);
    }
}
